var basepath="http://"+window.location.host;
function timeStamp2String (time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1;
    var date = datetime.getDate();
    var hour = datetime.getHours();
    var minute = datetime.getMinutes();
    var second = datetime.getSeconds();
    //var mseconds = datetime.getMilliseconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
};


//注意：数字与字符的转换会丢失多余的尾部0('123.010'<->123.01,'123.00'<->123)
var NumberFormat = {
    //按照每3位逗号分隔,字符串截子串法
    toCommaFormat: function(number){
        //如果是数字，则转换为字符串
        if(typeof number == 'number'){
            number = String(number)
        }

        var pointIndex = number.lastIndexOf('.'); //获取小数点的位置
        var hasDecimal = pointIndex != -1; //检查是否包含小数点
        var numberInteger = hasDecimal ? number.substring(0, pointIndex) : number; //获取数的整数部分
        var length = numberInteger.length;
        if(length > 3){
            var numberFormat = '';
            //从尾部往前截子串拼接
            for(var i= numberInteger.length; i> 0; i=i-3){
                //当前子串+之前拼接好的子串(因：substring(0,1) == substring(-2,1)，故无需判断i-3为负数的情况)
                numberFormat = numberInteger.substring(i-3,i) + (numberFormat==''?'':(','+numberFormat));
            }
            //整数部分+小数部分
            var numberDecimal = hasDecimal ? number.substring(pointIndex) : '';
            return numberFormat + numberDecimal;
        }else{
            //无需分隔，直接返回
            return number;
        }
    },
    //按照每3位逗号分隔,余数拼接法(缺点：有位数限制)
    toCommaFormat2: function(number){
        //如果是数字，则转换为字符串
        if(typeof number == 'number'){
            number = String(number)
        }

        var pointIndex = number.lastIndexOf('.'); //获取小数点的位置
        var hasDecimal = pointIndex != -1; //检查是否包含小数点
        var numberInteger = hasDecimal ? number.substring(0, pointIndex) : number; //获取数的整数部分
        var length = numberInteger.length;
        if(length > 3){
            var numberFormat = '';
            var remainder; //余数
            var quotient; //商
            while(true){
                quotient = Math.floor(Number(numberInteger)/1000); //商标识还需不需要进一步分隔
                remainder = Number(numberInteger)%1000; //余数即是分隔下来的子串
                numberFormat = String(remainder) + (numberFormat == ''?'':(','+numberFormat)); //当前余数拼接之前的余数
                if(quotient == 0){
                    //商为0，计算完毕
                    break;
                }
                //商作为被除数，进一步分隔
                numberInteger =  quotient;
            }
            //整数部分+小数部分
            var numberDecimal = hasDecimal ? number.substring(pointIndex) : '';
            return numberFormat + numberDecimal;
        }else{
            //无需分隔，直接返回
            return number;
        }

    },
    //正则式替换，来自网友
    format_number:function(nStr ){
        nStr += '';  //数字转换为字符
        x = nStr.split('.');  //按照小数点分隔
        x1 = x[0];  //整数部分
        x2 = x.length > 1 ? '.' + x[1] : '';  //小数部分
        var rgx = /(\d+)(\d{4})/;  //正则式定义
        while (rgx.test(x1)) {  //正则式匹配
            x1 = x1.replace(rgx, '$1' + ',' + '$2');   //正则式替换
        }
        return x1 + x2;
    }

};